package com.cloudptt.api.product.func.call.group.fsm.video.trans;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.models.call.RtcpMsg;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.CallTimer;
import com.cloudptt.api.product.func.call.group.fsm.IRollback;
import com.cloudptt.api.product.func.call.group.fsm.RollbackTimer;
import com.cloudptt.api.product.func.call.group.fsm.video.IVideoSubState;
import com.cloudptt.api.product.func.call.group.fsm.video.recv.VideoRecvAgent;
import com.cloudptt.api.product.log.Log;
//import com.cloudptt.api.product.func.call.group.fsm.CallAgent;

import android.content.Context;


public class VideoTransPendingRequestState implements IVideoSubState,IRollback
{
	private VideoTransAgent agent = null;
	private RollbackTimer rollbackTimer = null;
	private int c101 = 3;
	private int t101 = 1;

	public VideoTransPendingRequestState(Context ctx, VideoTransAgent agent)
	{
		CallTimer callTimer = agent.getCallTimer();
		this.c101 = callTimer.getC101();
		this.t101 = callTimer.getT101();

		rollbackTimer = new RollbackTimer(ctx,t101,this);

		this.agent = agent;
		Log.d(VideoTransAgent.Log_Key, "currentState is VideoPendingRequestState");
	}
	public int doUserAction(int btnAction)
	{
		return CauseCont.OperateErr_NoAgent;
	}
	public int doUserAction(int btnAction,String infoStr)
	{
		if(CallConst.BtnAction_Release == btnAction)
		{
//			int result = agent.transRelease();
			//			if(CauseCont.OperateCheck_Suc == result)
			int result = agent.sendTrans(CallAttr.RtcpMsg_Release, "", 0, 0);
			{
				agent.goToState( CallConst.VideoTransState_PendingRelease);
			}
			return result;
		}
		int result = CauseCont.OperateCheck_Suc;
		return result;
	}
	@Override
	public void receiveRtcpMsg(RtcpMsg msg) {
		// TODO Auto-generated method stub
		Log.d(VideoTransAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg msg = " + msg);
		if(msg == null)
		{
			Log.d(VideoTransAgent.Log_Key, "receiveRtcpMsg msg == null");
			return;
		}
		if(agent == null)
		{
			Log.d(VideoTransAgent.Log_Key, "receiveRtcpMsg agent == null");
			return;
		}

		String title = msg.getTitle();
		if(title.equals(RtcpMsg.Title_MCV1_REJECTED))
		{
			int cause = msg.getCause();
			Log.d(VideoTransAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg cause = " + cause);
			//			 agent.notifyDenyCause(cause);
			//			gotoNoPermissionStatus();
			agent.goToState(CallConst.VideoTransState_NoPermission);
		}
		else if(title.equals(RtcpMsg.Title_MCV1_GRANTED))
		{
			//			gotoPermissionStatus();
			agent.goToState(CallConst.VideoTransState_Permission);
		}
		if(title.equals(RtcpMsg.Title_MCV1_IDLE))
		{
			//			gotoNoPermissionStatus();
			agent.goToState(CallConst.VideoTransState_NoPermission);
//			agent.receiveRtcpMsg(msg);
		}
		else if(title.equals(RtcpMsg.Title_MCV1_TRANSM_MEDIA_NOTE))
		{
			String num = msg.getVideoTakenNum();//getValue(RtcpMsg.Key_McpttId);
			String jsonStr = msg.getTransInfoJson();
			Log.d(VideoTransAgent.Log_Key, "receiveRtcpMsg num = " + num + " jsonStr = " + jsonStr);
			this.agent.notifyTransUserInfo(num,jsonStr);
		}
		else if(title.equals(RtcpMsg.Title_MCV1_REVOKED))
		{
			int cause = msg.getCause();
			Log.d(VideoTransAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg cause = " + cause);
			int result = agent.sendTrans(CallAttr.RtcpMsg_Release, "", 0, 0);
			if(result == CallConst.Operate_Suc)
			{
				agent.goToState( CallConst.VideoTransState_NoPermission);
			}
		}
		else if(title.equals(RtcpMsg.Title_MCV1_TRANSM_MEDIA_END_NOTE))
		{
			String num = msg.getVideoTakenNum();//getValue(RtcpMsg.Key_McpttId);
			String jsonStr = msg.getTransInfoJson();
			Log.d(VideoTransAgent.Log_Key, "receiveRtcpMsg num = " + num + " jsonStr = " + jsonStr);
			this.agent.notifyTransEndUserInfo(num,jsonStr);
			agent.goToState( CallConst.VideoTransState_NoPermission);
		}
		else if(title.equals(RtcpMsg.Title_MCV2_TRANSM_MEDIA_END_RSP))
		{
			agent.goToState( CallConst.VideoTransState_NoPermission);
		}
		else if(title.equals(RtcpMsg.Title_MCV2_TRANSM_MEDIA_RECEP_END_NOTE))
		{
			String num = msg.getVideoTakenNum();//getValue(RtcpMsg.Key_McpttId);
//			String jsonStr = msg.getTransInfoJson();
			Log.d(VideoRecvAgent.Log_Key, "receiveRtcpMsg  num = " + num);
			this.agent.notifyMediaRecepEnd(num);
		}
	}

	public void destory()
	{
		if(rollbackTimer != null)
		{
			rollbackTimer.cancelTimer();
			rollbackTimer = null;
		}
		agent = null;
	}

	@Override
	public int getState() {
		// TODO Auto-generated method stub
		return CallConst.VideoTransState_PendingRequest;
	}

	@Override
	public void rollback() {
		// TODO Auto-generated method stub
		Log.d(VideoTransAgent.Log_Key, "" + CallConst.stateTag(getState()) + " onTimeout");
		if(c101 >= 0)
		{
			if(agent != null)
			{
//				int result = agent.TransRequest();
				int result = agent.sendTrans(CallAttr.RtcpMsg_Request, "", 0, 0);
				if(result == CallConst.Operate_Suc)
				{}
			}
			if(rollbackTimer != null)
			{
				rollbackTimer.restartTimer(t101);
			}
			c101 --;
		}
		else if(agent != null)
		{
			agent.goToState(CallConst.VideoTransState_NoPermission);
		}
	}
}
